package ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import storage.Pcb;



import controller.Index;

public class ProcUi {
	private static List<String> logcatStrings = new ArrayList<String>();
	public static List<String> runProcList = new ArrayList<String>();
	public static List<String> brockProcList = new ArrayList<String>();
	public static List<String> readyProcList = new ArrayList<String>();
	private JPanel mainJPanel = new JPanel(new GridLayout(2, 1, 50, 50));
	private JFrame procJFrame = new JFrame();

	// private static int i = 0;// test

	public ProcUi() {

		procJFrame.setSize(900, 750);

		procJFrame.setTitle("OS");
		procJFrame.setLocationRelativeTo(null);
		procJFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		procJFrame.setLayout(new BorderLayout());

		procLogPane jbTop = new procLogPane("进程管理日志", 1);

		procLogPane jbLeft = new procLogPane("就绪队列", 2);

		procLogPane jbCenter = new procLogPane("运行队列", 3);

		procLogPane jbRight = new procLogPane("阻塞队列", 4);

		JPanel mainJPanel1 = new JPanel(new GridLayout(1, 3, 50, 50));
		mainJPanel1.add(jbLeft);
		mainJPanel1.add(jbCenter);
		mainJPanel1.add(jbRight);

		mainJPanel.add(jbTop);

		mainJPanel.add(mainJPanel1);

		procJFrame.add(mainJPanel);
		

	}
	
	public void start(){
		procJFrame.setVisible(true);
	}
	
	public static List<String> getLogcatStrings() {
		return logcatStrings;
	}

	public static void setLogcatStrings(List<String> logcatStrings) {
		ProcUi.logcatStrings = logcatStrings;
	}
}

class procLogPane extends JPanel {
	private JLabel titleLabel = new JLabel();// 信息项标题
	private JTextArea contentlLabel = new JTextArea();// 信息项内容
	private Font font1 = new Font("黑体", 1, 20);
	private Font font2 = new Font("黑体", 1, 16);
	private String content = new String();
	// private static int testI = 0;
	private int type = 0;

	public procLogPane(String title, int type) {
		this.type = type;
		setLayout(new BorderLayout());
		titleLabel.setText(title);

		titleLabel.setFont(font1);
		JPanel jPanel = new JPanel();
		jPanel.setBackground(Color.green);

		contentlLabel.setText(content);
		contentlLabel.setFont(font2);
		contentlLabel.setBackground(Color.green);

		jPanel.add(contentlLabel);
		add(titleLabel, BorderLayout.NORTH);
		add(jPanel, BorderLayout.CENTER);
		Thread t=new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				run1();
			}
		});
		t.setDaemon(true);
		t.start();
	}

	private void run1() {
		while (true) {
			try {
				Pcb[] pcbs = controller.Ram.getObject().getPcbs();
				if (pcbs != null) {
					content = "";
					if (type == 1) {
						for (int i = 0; i < ProcUi.getLogcatStrings().size(); i++) {
							content = content
									+ ProcUi.getLogcatStrings().get(i) + "\n";
						}
						titleLabel.setText("进程管理日志" + "    "
								+ new Date().toString());
					} else if (type == 2) {// 就绪
						for (int i = 0; i < pcbs.length; i++) {
							if (i < 20 && pcbs[i].getStatus() != null) {
								if (pcbs[i].getStatus().equals("就绪态")) {
									content = content + pcbs[i].getPid() + "\n";
								}

							}
						}
						titleLabel.setText("就绪队列" + "    "
								+ new Date().getSeconds());
					} else if (type == 3) {// 运行
						for (int i = 0; i < pcbs.length; i++) {
							if (i < 20 && pcbs[i].getStatus() != null) {
								if (pcbs[i].getStatus().equals("运行态")) {
									content = content + pcbs[i].getPid() + "\n";
								}
							}
						}
						titleLabel.setText("运行队列" + "    "
								+ new Date().getSeconds());
					} else {// 阻塞
						for (int i = 0; i < pcbs.length; i++) {
							if (i < 20 && pcbs[i].getStatus() != null) {
								if (pcbs[i].getStatus().equals("阻塞态")) {
									content = content + pcbs[i].getPid() + "\n";
								}

							}
						}
						titleLabel.setText("阻塞队列" + "    "
								+ new Date().getSeconds());
					}
					contentlLabel.setText(content);
					repaint();
				}
				Thread.sleep(1000);
			} catch (InterruptedException e) {

			}

		}
	}

	public JLabel getTitleLabel() {
		return titleLabel;
	}

	public void setTitleLabel(JLabel titleLabel) {
		this.titleLabel = titleLabel;
	}

}